<template>
    <div>
        <van-nav-bar title="登录" />

        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="手机号" label="手机号" placeholder="手机号"
                    :rules="[{ required: true, message: '手机号不正确' }]" />
                <van-field v-model="password" type="password" name="密码" label="密码" placeholder="密码"
                    :rules="[{ required: true, message: '密码不正确' }]" />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    登录
                </van-button>
            </div>
        </van-form>

    </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router';
import { loginApi } from '../request/api'
import { showToast } from 'vant'
const password = ref('');
const username = ref('');
const router=useRouter()
const onSubmit = () => {
    loginApi(username.value,password.value).then(res=>{
        if(res.data.code==0){
            localStorage.setItem('token',res.data.data.token)
            localStorage.setItem('username',res.data.data.username)
            showToast({
                type:'success',
                message:'登录成功',
                onClose(){
                    router.push('/home/my')
                }
            })
        }else{
            showToast({
                type:'success',
                message:'登录失败',
                onClose(){
                    username.value='',
                    password.value=''
                }
            })
        }
    })
};

</script>

<style lang="scss" scoped></style>